############## 
############## 
############## Models
remove(list = ls())

base::library(texreg)
base::library(conflicted)
base::library(tidyverse)
conflict_prefer("filter","dplyr")
base::library(ggplot2)
base::library(dplyr)
base::library(here)
conflict_prefer("here", "here")
base::library(systemfit)
base::library(stargazer)
#base::library(texreg)
base::library(DataCombine)
base::library(fixest)
base::library(ggeffects)

load(here("Data", "Model_Data.RData"))
glimpse(final_data)

summary(final_data$death_rate)
table(final_data$year)

final_data <- final_data %>% 
  mutate(time_trend = ifelse(year == 2004, 1, NA),
         time_trend = ifelse(year == 2008, 2, time_trend),
         time_trend = ifelse(year == 2012, 3, time_trend),
         time_trend = ifelse(year == 2016, 4, time_trend),
         time_trend = ifelse(year == 2020, 5, time_trend)) %>% 
  mutate(rural_urban2 = as.factor(rural_urban))


glimpse(final_data)


summary(ols_model2 <- lm(rep_share2 ~ death_rate + unemp_rate + 
                           log(defl_pcincome) + 
                           share_black + share_hisp + share_asian + share_young + share_elder + 
                           log1p(share_manuf) +
                           log(tot_pop) + rural_urban2 +
                           state + state_partiesdiff + rep_inc + incumbent_cand + lag_rep_share2 + 
                           time_trend*log1p(share_college),
                         data = final_data))



summary(ols_model3 <- lm(rep_share2 ~ death_rate + unemp_rate + 
                           log(defl_pcincome) + 
                           share_black + share_hisp + share_asian + share_young + share_elder + 
                           log1p(share_manuf) +
                           log(tot_pop) +
                           state + state_partiesdiff + rep_inc + incumbent_cand + lag_rep_share2 + 
                           time_trend*(rural_urban) + log1p(share_college) ,
                         data = final_data))


stargazer(ols_model2, ols_model3, 
          type = "latex",
          title = "OLS and Fixed Effect Estimates",
          dep.var.labels = c("Republican Vote Share", "Republican Vote Share", "log(Dem/Rep)", "log(Abs/Rep)"), 
          no.space = FALSE, single.row = T,
          covariate.labels = c("Overdose Death Rate",
                               "Local Unemployment Rate",
                               "Log PC Income",
                               "Share Black",
                               "Share Hispanic",
                               "Share Asian",
                               "Share Young Voters",
                               "Share Elder Voters",
                               "Log Share Manufacture",
                               "Log Population",
                               "Close Election (State)",
                               "Republican Presidency",
                               "Incumbent Candidate",
                               "Lagged Republican Vote Share",
                               "Time Trend",
                               "Log Share College Degree",
                               "Time Trend*Log Share College Degree",
                               "Time Trend*Rural-Urban Code"),
          omit = c("rural_urban2", "state"),
          omit.labels = c("Rural-Urban Code",
                          "State Fixed Effects"))


